package com.sunwayworld.basemodule.rm.train.exam.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.sunwayworld.basemodule.rm.train.exam.bean.LimsCoreExamBean;
import com.sunwayworld.basemodule.rm.train.exam.dao.LimsCoreExamDao;
import com.sunwayworld.basemodule.rm.train.exam.service.LimsCoreExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.audit.constant.AuditConstant;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.context.LocalContextHelper;
import com.sunwayworld.framework.exception.InvalidDataException;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.module.mdm.user.bean.CoreUserBean;

@GikamBean
@Repository
public class LimsCoreExamServiceImpl implements LimsCoreExamService {
    @Autowired
    private LimsCoreExamDao limsCoreExamDao;
    
    @Override
    @SuppressWarnings("unchecked")
    public LimsCoreExamDao getDao() {
        return limsCoreExamDao;
    }
    
    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    @Audit(AuditConstant.INSERT)
    public Long insert(RestJsonWrapperBean wrapper) {
        LimsCoreExamBean exam = wrapper.parseUnique(LimsCoreExamBean.class);
        
        exam.setId(ApplicationContextHelper.getNextIdentity());
        exam.setLastUpdatedById(LocalContextHelper.getLoginUserId());
        exam.setLastUpdatedByName(LocalContextHelper.getLoginUserName());
        exam.setLastUpdatedTime(LocalDateTime.now());
        
        getDao().insert(exam);
        
        return exam.getId();
    }
    
    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.UPDATE)
    @Audit(AuditConstant.SAVE)
    public void updateIfChanged(List<LimsCoreExamBean> itemList) {
        CoreUserBean currentUser = LocalContextHelper.getLoginUser();
        LocalDateTime currentTime = LocalDateTime.now();
        
        itemList.forEach(e -> {
            e.setLastUpdatedById(currentUser.getId());
            e.setLastUpdatedByName(currentUser.getUserName());
            e.setLastUpdatedTime(currentTime);
        });
        
        LimsCoreExamService.super.updateIfChanged(itemList);
    }
    
    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.ACTIVATE)
    @Audit(AuditConstant.ACTIVATE)
    public void activate(RestJsonWrapperBean wrapper) {
        List<LimsCoreExamBean> itemList = wrapper.parse(getDao().getType());
        
        if (!doValid(itemList)) {
            throw new InvalidDataException("CORE.MODULE.TRAIN.EXAM.TIP.LOCKED_FAILED");
        }
        
        itemList.forEach(i -> setActivateProperties(i, true));
        itemList.forEach(e -> {
            e.setLastUpdatedById(LocalContextHelper.getLoginUserId());
            e.setLastUpdatedByName(LocalContextHelper.getLoginUser().getUserName());
            e.setLastUpdatedTime(LocalDateTime.now());
        });
        
        getDao().update(itemList, "ACTIVATEDFLAG", "ACTIVATEDBYID", "ACTIVATEDBYNAME", "ACTIVATEDTIME",  "LASTUPDATEDBYID", "LASTUPDATEDBYNAME", "LASTUPDATEDTIME");
        
    }
    
    private boolean doValid(List<LimsCoreExamBean> itemList) {
        boolean validFlag = true;
        
        for (LimsCoreExamBean exam : itemList) {
            if ("manual".equals(exam.getQuestionMethod())) {
                if (exam.getTmplSingleChoiceQty().intValue() != exam.getSingleChoiceQty().intValue() 
                        || exam.getTmplMultipleChoiceQty().intValue() != exam.getMultipleChoiceQty().intValue()
                        || exam.getTmplJudgementQty().intValue() != exam.getJudgementQty().intValue()
                        || exam.getTmplShortAnswerQty().intValue() != exam.getShortAnswerQty().intValue()
                        || exam.getTmplCompletionQty().intValue() != exam.getCompletionQty().intValue()) {
                    validFlag = false;
                    break;
                }
            }
        }
        
        return validFlag;
    }
}